package com.qf.Web.Servlet;

import com.qf.Domain.Goods;
import com.qf.Domain.PageBean;
import com.qf.Service.GoodService;
import com.qf.Service.Impl.GoodsServiceImpl;
import com.qf.Utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/*
 *
 */
@WebServlet(name = "GoodsServlet",value = "/goodsservlet")
public class GoodsServlet extends BaseServlet {
    //分页查询
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response){
        try {
            //获取分页信息
            String num = request.getParameter("pageNum");
            String size = request.getParameter("pageSize");
            int pageNum=1;
            int pageSize=8;
            if (!StringUtils.isEmtry(num)){
                pageNum=Integer.parseInt(num);
                if (pageNum<1){
                    pageNum=1;
                }
            }
            if (!StringUtils.isEmtry(size)){
                pageSize=Integer.parseInt(size);
                if (pageSize<1){
                    pageSize=8;
                }
            }
            String typeId = request.getParameter("typeId");
            //System.out.println(typeId);
            String name = request.getParameter("name");
            String where=" where 1=1";
            ArrayList<Object> params=new ArrayList<>();
            if (!StringUtils.isEmtry(typeId)){
                where+=" and typeid=?";
                int typeid=Integer.parseInt(typeId);
                params.add(typeid);
            }
            if (!StringUtils.isEmtry(name)){
                where+=" and name=? like ?";
                params.add(name);
            }
            //System.out.println(where);
            //System.out.println(params.toString());
            GoodService goodsService=new GoodsServiceImpl();
            PageBean<Goods> pageBean=goodsService.select(pageNum, pageSize, where, params);
            //System.out.println(pageBean.toString());
            request.setAttribute("pageBean",pageBean);
            request.setAttribute("typeId",typeId);
            return "/goodsList.jsp";
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg","商品查询失败");
            return "/message.jsp";
        }
    }
    //商品详情
    public String getGoodsById(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        if (StringUtils.isEmtry(id)){
            request.setAttribute("msg", "商品id为空");
            return "/message.jsp";
        }
        GoodService goodsService=new GoodsServiceImpl();
        try {
            int i=Integer.parseInt(id);
            Goods goods=goodsService.fingById(i);
            request.setAttribute("goods", goods);
            return "/goodsDetail.jsp";
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询商品失败");
            return "/message.jsp";
        }
    }
}
